import 'dart:convert';

T? asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }
  return null;
}

class User {
  User({
    this.userId,
    this.gender,
    this.avatar,
    this.status,
    this.username,
    this.password,
    this.phone,
    this.createTime,
  });

  factory User.fromJson(Map<String, dynamic> jsonRes) => User(
        userId: asT<String?>(jsonRes['userId']),
        gender: asT<int?>(jsonRes['gender']),
        avatar: asT<String?>(jsonRes['avatar']),
        status: asT<int?>(jsonRes['status']),
        username: asT<String?>(jsonRes['username']),
        password: asT<String?>(jsonRes['password']),
        phone: asT<String?>(jsonRes['phone']),
        createTime: asT<int?>(jsonRes['createTime']),
      );

  String? userId;
  int? gender;
  String? avatar;
  int? status;
  String? username;
  String? password;
  String? phone;
  int? createTime;

  @override
  String toString() {
    return jsonEncode(this);
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'userId': userId,
        'gender': gender,
        'avatar': avatar,
        'status': status,
        'username': username,
        'password': password,
        'phone': phone,
        'createTime': createTime,
      };

  User clone() =>
      User.fromJson(asT<Map<String, dynamic>>(jsonDecode(jsonEncode(this)))!);
}
